package com.woniuxy.controller;

import com.alibaba.fastjson.JSON;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.conmon.MyPageData;
import com.woniuxy.conmon.enums.SystemCode;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.pojo.Category;
import com.woniuxy.pojo.dto.CategoryDTO;
import com.woniuxy.pojo.vo.CategoryVO;
import com.woniuxy.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/category")
@Api("品类管理")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("品类查询")
    @GetMapping("/getAll/{pageSize}/{pageNum}")
    public ResponseEntity<?> getAll(@PathVariable Integer pageSize,
                                    @PathVariable Integer pageNum){

        Page<Category> page = categoryService.getAll(pageSize,pageNum);
        List<Category> categories = page.getRecords();
        List<CategoryVO> vos = categories.stream()
                .map(category -> {
                    CategoryVO vo = new CategoryVO();
                    BeanUtils.copyProperties(category, vo);
                    return vo;
                }).collect(Collectors.toList());

        MyPageData data = new MyPageData(page.getTotal(),vos);

        return ResponseEntity.ok(data);
    }

    @ApiOperation("新增品类")
    @PostMapping("/addCategory")
    public ResponseEntity<?> addCategory(@RequestBody Map<String,Object> playload){
        Category category = getCategory(playload);
        categoryService.addCategory(category);
        return ResponseEntity.ok(SystemCode.ADDCATEGORY_SUCCESS.getMessage());

    }
    @ApiOperation("根据id查询品类信息")
    @GetMapping("/getCategoryById/{categoryId}")
    public ResponseEntity<?> getCategoryById(@PathVariable Long categoryId){
        CategoryVO categoryVO = categoryService.getCategoryById(categoryId);
        return ResponseEntity.ok(categoryVO);
    }


    @ApiOperation("品类修改")
    @PutMapping("/updateCategory/{id}")
    public ResponseEntity<?> updateCategory(@RequestBody Map<String,Object> playload,
                                            @PathVariable Long id){

        Category category = getCategory(playload);

        categoryService.updateCategory(category,id);
        return ResponseEntity.ok(SystemCode.UPDATECATEGORY_SUCCESS.getMessage());
    }

    private Category getCategory(Map<String, Object> playload) {
        String categoryName = (String)playload.get("categoryName") ;
        List<String> subCategories = (List<String>)playload.get("subCategories");
        String joined = JSON.toJSONString(subCategories);
        Integer sortOrder = (Integer)playload.get("sortOrder");
        String description = (String)playload.get("description");
        Category category = new Category();
        category.setCategoryName(categoryName);
        category.setDescription(description);
        category.setSubCategories(joined);
        category.setSortOrder(sortOrder);
        return category;
    }

    @ApiOperation("删除品类")
    @DeleteMapping("/deleteCategoryById/{id}")
    public ResponseEntity<?> deleteCategoryById(@PathVariable Long id){
        categoryService.deleteCategoryById(id);
        return ResponseEntity.ok(SystemCode.DELETECATEGORY_SUCCESS.getMessage());
    }

    @ApiOperation("下架品类")
    @DeleteMapping("/removeCategoryByIds/{ids}")
    public ResponseEntity<?> removeCategoryByIds(@PathVariable List<Long> ids){
        categoryService.removeCategoryByIds(ids);
        return ResponseEntity.ok(SystemCode.REMOVECATEGORY_SUCCESS.getMessage());
    }

    @ApiOperation(("查询一级品类"))
    @GetMapping("/getCategoryName")
    public ResponseEntity<?> getCategoryName(){
        List<String> names = categoryService.getCategoryName();
        return ResponseEntity.ok(names);
    }

    @ApiOperation("根据品类查子品类")
    @GetMapping("/getSubByCategory/{categoryName}")
    public ResponseEntity<?> getSubByCategory(@PathVariable String categoryName) throws Exception {
        String names = categoryService.getSubByCategory(categoryName);
        List<String> list = convertJsonStringToList(names);
        return ResponseEntity.ok(list);

    }
    public List<String> convertJsonStringToList(String jsonString) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(jsonString, new TypeReference<List<String>>() {});
    }

    @ApiOperation("综合查询")
    @PostMapping("/getByCategoryAndTime")
    public ResponseEntity<?> getByCategoryAndTime(@RequestBody CategoryDTO categoryDTO){
        List<CategoryVO> vos = categoryService.getByCategoryAndTime(categoryDTO);
        return ResponseEntity.ok(vos);
    }

}
